package com.demo.core.controller;

import com.demo.base.model.user.Actions;
import com.demo.base.model.user.Roles;
import com.demo.base.util.CommonUtil;
import com.demo.core.service.ActionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 权限控制，角色赋予菜单权限，用户关联角色，角色添加
 * Created by jiandandia on 2017/4/7.
 */
@RequestMapping("/Actions")
@Controller
public class ActionsController {
    @Autowired
    private ActionsService actionsService;

    /**
     * 添加或修改叫角色
     */
    @ResponseBody
    @RequestMapping("/saveRole")
    public String saveRole(@RequestBody Roles roles){
        try {
            actionsService.saveRole(roles);
        }catch (Exception e){
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @RequestMapping("/deleteRoleById")
    public String deleteRoleById(@RequestBody String roleId){
        try{
            actionsService.deleteRoleById(roleId);
        }catch(Exception e){
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * 为角色添加菜单权限
     */
    @ResponseBody
    @RequestMapping("/saveRoleActions")
    public String saveRoleActions(@RequestBody String roleId, String actionIds){
        try {
            String[] _actionIds = actionIds.indexOf(",") > 0 ? actionIds.split(",") : new String[]{actionIds};
            actionsService.saveRoleActions(roleId, _actionIds);
        }catch (Exception e){
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * 获取角色的菜单权限
     */
    @ResponseBody
    @RequestMapping("/getRoleActions")
    public List<Actions> getRoleActions(@RequestBody String roleId){
            return actionsService.getRoleActions(roleId);
    }

    /**
     * 角色关联用户(一个角色可以关联多个用户)
     */
    @ResponseBody
    @RequestMapping("/newRoleUsers")
    public String newRoleUsers(@RequestBody String roleId, String userIds){
        try {
            String[] _userIds = userIds.indexOf(",") > 0 ? userIds.split(",") : new String[]{userIds};
            actionsService.newRoleUsers(roleId, _userIds);
        }catch (Exception e){
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }

    /**
     * 删除用户赋予的角色(可同时删除多个用户对应的角色，传入角色Id,传入需要删除的用户id)
     */
    @ResponseBody
    @RequestMapping("/deleteRoleUsers")
    public String deleteRoleUsers(@RequestBody String roleId, String userIds){
        String[] _userIds = userIds.indexOf(",") > 0 ? userIds.split(",") : new String[]{userIds};
        try{
            actionsService.deleteRoleUsers(roleId, _userIds);
        }catch(Exception e){
            return CommonUtil.FAIL;
        }
        return CommonUtil.SUCCESS;
    }
}
